/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.article.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.Article;
import com.rzico.article.entity.ArticleLaud;
import com.rzico.article.service.ArticleLaudService;
import com.rzico.article.service.ArticleService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 笔记点赞控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-08
 */
@Api(description = "笔记点赞接口")
@RestController
@RequestMapping("/member/articleLaud")
public class ArticleLaudMemberController extends BaseController {

    @Autowired
    private ArticleLaudService articleLaudService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private MemberService memberService;




    /**
     * 保存笔记点赞
     *
     * @param articleLaud
     * @return
     */
    @ApiOperation("保存笔记点赞")
    @PostMapping("/save")
    public CommResult<ArticleLaud> save(@RequestBody ArticleLaud articleLaud) {
        Member member = memberService.getCurrent();
        if (null == member) {
            return CommResult.error("请登录后点赞");
        }
        ArticleLaud result = articleLaudService.saveArticleLaud(member, articleLaud);
        return CommResult.success(result);

    }

    /**
     * 查看我的点赞
     */
    @ApiOperation("查看我的点赞")
    @GetMapping("/find")
    public CommResult<ArticleLaud> show(Pageable pageable) {
        Member member = memberService.getCurrent();

        Map<String,Object> params = new HashMap<>();
        params.put("memberId", member.getId());

        //分页查询点赞表文章的id,根据我的memberId
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ArticleLaud> articleLauds = articleLaudService.selectList(params);
        PageResult<ArticleLaud> pageResult = new PageResult<ArticleLaud>(articleLauds, startPage.getTotal(), pageable);

        //List<Long> collect = articleLauds.stream().map(a -> a.getArticleId()).collect(Collectors.toList());
        Map<Long, Object> articleIds = articleLauds.stream().collect(Collectors.toMap(ArticleLaud::getArticleId, null));
        params.remove("memberId");
        params.put("articleIds", articleIds);
        List<Article> articles = articleService.selectList(params);

        return CommResult.success(articles);
    }
}
